package com.xiao.androidcompose01.viewmodel

import com.tencent.trtc.TRTCCloud
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json

object TencentMsgUtils {
   //孩子发送数据到定位
   fun sendLocation(cloud: TRTCCloud,latitude: Double,longitude: Double,address: String,time: Long){
//      val msg = TencentMsg.Location(latitude,longitude,address)
//      val array = Json.encodeToString(msg).toByteArray()
//      val result = cloud.sendCustomCmdMsg(3,array,true,true)
      val locationSuccess = ChildReplyCmd.Location.Success(latitude,longitude,address,time)
      val array = Json.encodeToString(locationSuccess).toByteArray()
      val result = cloud.sendCustomCmdMsg(3,array,true,true)
      println("信息发送:${result}  ${locationSuccess}")
   }
   
   fun sendScreenCapture(cloud: TRTCCloud){
   
   }
   
   fun filterMsg(code: Int,msg: ByteArray): ChildReplyCmd? {
      if (code==3){
         return Json.decodeFromString<ChildReplyCmd.Location.Success>(String(msg,Charsets.UTF_8))
      }else{
         return null
      }
   }
   
}

const val CMD_SCREEN_CAPTURE = 1 //命令：屏幕共享给父端
const val CMD_CAMERA_CAPTURE = 2 //命令：摄像机和麦克风 共享给父端，子端也能看到
const val CMD_LOCATION = 3 //命令：地址共享给父端

sealed class ParentSendCmd(val code: Int){
   data class ScreenCapture(val message: String): ParentSendCmd(CMD_SCREEN_CAPTURE)
   data class CameraCapture(val message: String): ParentSendCmd(CMD_CAMERA_CAPTURE)
   data class Location(val latitude: Double, val longitude: Double, val address: String): ParentSendCmd(CMD_LOCATION)
}

sealed class ChildReplyCmd(val code: Int){
   sealed class ScreenCapture : ChildReplyCmd(CMD_SCREEN_CAPTURE){
      data object Success: ScreenCapture()
      data object Failed: ScreenCapture()
   }
   sealed class CameraCapture : ChildReplyCmd(CMD_CAMERA_CAPTURE){
      data object Success: CameraCapture()
      data object Failed: CameraCapture()
   }
   sealed class Location : ChildReplyCmd(CMD_LOCATION){
      @Serializable
      data class Success(val latitude: Double, val longitude: Double, val address: String,val time:Long): Location()
      data object Failed: Location()
   }
   
   fun ifLocationSuccess(block: Location.Success.() -> Unit){
      if (this is Location.Success){
         block(this)
      }
   }
   
   fun ifLocationFailed(block: Location.Failed.() -> Unit){
      if (this is Location.Failed){
         block(this)
      }
   }
}

@Serializable
sealed class TencentMsg(val isParentSend: Boolean,val code: Int) {
   data object ScreenCapture //屏幕录屏
   //地址信息
   @Serializable
   data class Location(val latitude: Double, val longitude: Double, val address: String): TencentMsg(false,3)
}